package org.travel_system.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.travel_system.pojo.Admin;
import org.travel_system.pojo.Reservation;
import org.travel_system.pojo.Room;
import org.travel_system.pojo.User;
import org.travel_system.service.AdminService;
import org.travel_system.service.HotelService;
import org.travel_system.service.ReservationService;
import org.travel_system.service.UserService;
import org.travel_system.utils.Result;
import org.travel_system.utils.ThreadLocalUtil;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/reservations")
public class ReservationController {

    private final ReservationService reservationService;
    private final AdminService adminService;
    private final UserService userService;
    private final HotelService hotelService;

    @Autowired
    public ReservationController(ReservationService reservationService, AdminService adminService, UserService userService, HotelService hotelService) {
        this.reservationService = reservationService;
        this.adminService = adminService;
        this.userService = userService;
        this.hotelService = hotelService;
    }

    //查询所有预订信息
    @GetMapping
    public Result<List<Reservation>> getReservations() {

        Map<String, Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        User u = userService.findUserByName(username);
        Admin admin = adminService.findAdminByName(username);

        if (admin != null) {
            //管理员查询所有预订信息
            return Result.success(reservationService.findAllReservations());
        }

        //用户查询
        int userId = userService.findUserId(username);
        return Result.success(reservationService.findUserReservations(userId));
    }

    //查询单个预订信息
    @GetMapping("/{id}")
    public Result<Reservation> getReservationById(@PathVariable int id) {
        return Result.success(reservationService.findReservationById(id));
    }

    //创建预订信息
    @PostMapping
    public Result<?> createReservation(@RequestBody Reservation reservation) {
        Map<String, Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        int userId = userService.findUserId(username);
        reservation.setTouristId(userId);
        reservation.setIsReserved(1);
        Room room = hotelService.getRoomByRoomNumber(reservation.getHotelName(),reservation.getRoomNumber());
        room.setIsBooked(1);
        hotelService.updateRoom(room);
        return reservationService.createReservation(reservation) > 0 ?
                Result.success() :
                Result.error("创建预订信息失败！");
    }

    //修改预订信息
    @PutMapping()
    public Result<?> updateReservation( @RequestBody Reservation reservation) {
        return reservationService.updateReservation(reservation) > 0 ?
                Result.success() :
                Result.error("修改预订信息失败！");
    }

    //删除预订信息
    @DeleteMapping("/{id}")
    public Result<?> deleteReservation(@PathVariable int id) {
        return reservationService.deleteReservation(id) > 0 ?
                Result.success() :
                Result.error("删除预订信息失败！");
    }
}
